package br.ufmg.dcc.simulesspl.fsm;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;

public class FSMTest {

	@Test
	public void testFSMFlow() {
		FSM fsm = new FSM();
		TestState ts1 = new TestState();
		TestState ts2 = new TestState();
		
		assertThat(fsm.getCurrentState(), equalTo(null));
		
		fsm.goTo(ts1);
		assertThat(fsm.getCurrentState(), equalTo((AbstractState)ts1));
		assertThat(ts1.startExecuted, equalTo(true));
		assertThat(ts1.runExecuted, equalTo(false));
		assertThat(ts1.finishExecuted, equalTo(false));
		
		fsm.run();
		assertThat(ts1.startExecuted, equalTo(true));
		assertThat(ts1.runExecuted, equalTo(true));
		assertThat(ts1.finishExecuted, equalTo(false));
		
		fsm.goToAndRun(ts2);
		assertThat(ts1.startExecuted, equalTo(true));
		assertThat(ts1.runExecuted, equalTo(true));
		assertThat(ts1.finishExecuted, equalTo(true));
		assertThat(ts2.startExecuted, equalTo(true));
		assertThat(ts2.runExecuted, equalTo(true));
		assertThat(ts2.finishExecuted, equalTo(false));
		
		fsm.finish();
		assertThat(ts2.startExecuted, equalTo(true));
		assertThat(ts2.runExecuted, equalTo(true));
		assertThat(ts2.finishExecuted, equalTo(true));
	}
	
	static class TestState extends AbstractState {
		
		public boolean runExecuted = false;
		public boolean startExecuted = false;
		public boolean finishExecuted = false;

		@Override
		public void run() {
			runExecuted = true;
		}

		@Override
		public void start() {
			startExecuted = true;
		}

		@Override
		public void finish() {
			finishExecuted = true;
		}
		
	}

}
